#!/usr/bin/python
#coding:utf-8 

# 参考 https://blog.csdn.net/z497896143/article/details/81073044

from flask import Blueprint, Flask, jsonify,request
from werkzeug.utils import secure_filename
import time
from flask_docs import ApiDoc
from Commons.filePath import FilePath
bgimgPath=FilePath("/static/bgimg/").path()

app = Flask(__name__)

# Using CDN
# app.config["API_DOC_CDN"] = True
# Disable document pages
# app.config["API_DOC_ENABLE"] = False
# Methods allowed to be displayed
# app.config["API_DOC_METHODS_LIST"] = ["GET", "POST", "PUT", "DELETE", "PATCH"]
# Custom url_prefix
app.config["API_DOC_URL_PREFIX"] = "/docs/"
# Api Document needs to be displayed
app.config["API_DOC_MEMBER"] = ["upload", "captcha","hello"]

ApiDoc(app, title="Captcha API", version="1.0.3")

upload = Blueprint("upload", __name__)
captcha = Blueprint("captcha", __name__)

hello = Blueprint("/", __name__)


@captcha.route("/check", methods=["POST"])
def checkData():
    """ 将接收的数据进行解密验证
    @@@
    ### 参数
    |  args | nullable | request type | type |  remarks |
    |-------|----------|--------------|------|----------|
    | token |  false   |    body      | str  | 数据加密返回   |
    | timestamp  |  false   |    body      | str  |  时间戳  |
    ### 请求
    ```json
    {"token": "tokentokentokentokentokentokentoken", "timestamp": "24233333353535435"}
    ```
    ### 响应
    ```json
    {"code": 200,"result":true, "msg": "", "data": null}
    {"code": 200,"result":false, "msg": "验证失败", "data": null}
    {"code": 200,"result":false, "msg": "参数不能为空", "data": null}
    {"code": 200,"result":false, "msg": "请稍候再试", "data": null}
    ```
    @@@
    """
    print(request.form,"info")
    return jsonify({"api": "add data"})


@captcha.route("/get", methods=["GET"])
def getCapycha():
    """获取验证码图片
    @@@
    ### 参数
    无
   
    ### 请求
    ```
    /api/get
    ```
    ### 返回
    ```json
    {"code": 200,"result":true ,"msg": "", "data": {
        bg:"helloworld.png",
        gapDiagram:"gapdiagram.png",
        token:"sfsfwkjriowrwrjlwuisw",加密x,y 轴数据 
    }}
    {"code": 200,"result":false, "msg": "您验证次数过多,请重新尝试,在1秒钟刷新过多次", "data": {
        bg:"",
        gapDiagram:"",
        token:"wrwerwrwerwrwe" 无效的token 
    }}
    {"code": 500,"result":false, "msg": "请稍候再试", "data":  {
        bg:"",
        gapDiagram:"",
        token:"wrwerwrwerwrwe" 无效的token 
    }}
    ```
    @@@
    """

    return jsonify({"api": "delete data"})


@upload.route("/file", methods=["POST"])
def uploadImg():
    """
        验证码图片上传
        @@@
        ### description
        > 上传图片
        ### args
        |  args | nullable | request type | type |  remarks |
        |-------|----------|--------------|------|----------|
        |  pathimage |  false   |    body      | image  | 图片 |
        |  timestamp |  false   |    body      | string  | 上传时间戳(毫秒级) |
        ### request
        ```json
        {"pathimage": "bitter", "timestamp": "139472874782323"}
        ```
        ### return
        ```json
        {"code": 200,"status":true, "msg": "上传成功", "data": null}
        {"code": 200,"status":false,"msg": "上传失败", "data": null}
        ```
        @@@
    """


    if request.method == 'POST':
    # 获取post过来的文件名称，从name=file参数中获取

        print(request.files,"info")
    
        print(request.form,"时间戳")

        return jsonify({"code": 200,"status":True, "msg": "上传成功", "data": None})

        print(timestamp,"时间戳")
        print(file)
        # secure_filename方法会去掉文件名中的中文
        file_name = secure_filename(file.filename)
      # 保存图片
        file_path = bgimgPath+timestamp
        # file.save(file_path) 


    # img = request.files.get('pathimage')
    # timestamp = request.form.get("timestamp")
    # if img ==None:
    #     return  jsonify({"code": 200,"status":False,"msg": "图片必填", "data": None})
    
    # if timestamp ==None:
    #     return  jsonify({"code": 200,"status":False,"msg": "时间戳必填", "data": None})
    
    # t = time.time()
    # timestamp= int(round(t * 1000))    #毫秒级时间戳 
    # file_path = bgimgPath+timestamp
    # img.save(file_path) 

    return jsonify({"code": 200,"status":True, "msg": "上传成功", "data": None})


@hello.route("/", methods=["GET"])
def helloWorld():
    """ 默认返回, 替换找不到路由这个问题 """
    return jsonify({"code":200,"message":"文档参考","url": "http://127.0.0.1:8888/docs/","data":None})


 
app.register_blueprint(hello, url_prefix="/")
app.register_blueprint(upload, url_prefix="/upload")
app.register_blueprint(captcha, url_prefix="/captcha")
app.run(host="0.0.0.0", port=8888, debug=True)
